import Vue from 'vue'
import Vuex from 'vuex'
import user from './user'
import dayjs from 'dayjs'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        /**
         * 是否需要强制登录
         */
        forcedLogin: false,
        hasLogin: false,
        userName: "zzz",
        token: null,
        years: [],
        yearsValue: [],
        months: ['1月', '’2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
        monthsValue: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
        yearsIndex: 0,
        monthIndex: 0,
        hiddeAppFun: false, //是否隐藏APP功能
    },
    mutations: {
        SET_OBJ(state, obj) {
            state.forcedLogin = obj.forcedLogin;
            state.hasLogin = obj.hasLogin;
            state.userName = obj.userName;
        },
        SET_YEARS(state, obj) {
            console.log('year', obj)
            state.years = obj.years
            state.yearsValue = obj.yearsValue
        },
        SET_MONTH(state, obj) {
            console.log('month', obj)
            state.months = obj.months
            state.monthsValue = obj.monthsValue
        },
        SET_INDEX(state, obj) {
            state.yearsIndex = obj.yearsIndex
            state.monthIndex = obj.monthIndex
        },
        SET_YEARS_INDEX(state, index) {
            state.yearsIndex = index
        },
        SET_MONTH_INDEX(state, index) {
            state.monthIndex = index
        }
    },
    actions: {
        setObj(context, obj) {
            console.log('setObj')
            context.commit('SET_OBJ', obj);
        },
        initApp(context) {
            console.log('initYear')
        },
        // 
        /**
         * 初始化年和月份
         * @param {Object} context
         * @param {Object} isAll 是否显示全部时间
         * @param {Object} isAuto 是否自动选择
         */
        initSelectYearAndMonth(context, param = {
            isAll: false,
            isAuto: true
        }) {

            const year = 2020
            const years = []
            const yearsValue = []
            const defaultMonth = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];
            const defaultMonthValue = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
            let month = [];
            let monthValue = [];
            if (param.isAll) {
                years.push('全部时间');
                yearsValue.push(-1);
                month.push('全部时间');
                monthValue.push(-1);
                month = month.concat(defaultMonth);
                monthValue = monthValue.concat(defaultMonthValue);
            } else {
                month = month.concat(defaultMonth);
                monthValue = monthValue.concat(defaultMonthValue);
            }
            years.push(year + '年')
            yearsValue.push(year)
            console.log('years =', years)
            if (dayjs().year() > year) {
                let num = dayjs().year() - year
                for (var i = 1; i <= num; i++) {
                    let addYear = year + i
                    years.push(addYear + '年')
                    yearsValue.push(addYear)
                }
            }
            context.commit('SET_YEARS', {
                years: years,
                yearsValue: yearsValue
            })
            context.commit('SET_MONTH', {
                months: month,
                monthsValue: monthValue
            })
            context.dispatch('clearSelect')
            console.log('isAuto =', param.isAuto)
            if (param.isAuto) {
                const {
                    state
                } = context
                const year = dayjs().year()
                const month = dayjs().month() + 1
                const obj = {
                    yearsIndex: 0,
                    monthIndex: 0
                }
                for (var i = 0; i < state.yearsValue.length; i++) {
                    if (year == state.yearsValue[i]) {
                        obj.yearsIndex = i
                    }
                }
                // console.log('state.monthsValue =', state.monthsValue.length)
                for (var i = 0; i < state.monthsValue.length; i++) {
                    // console.log('state.monthsValue[i] =', state.monthsValue[i])
                    if (month == state.monthsValue[i]) {
                        obj.monthIndex = i
                    }
                }
                context.commit('SET_INDEX', obj)
                console.log('obj =', obj)
            }

        },
        // 清除选择
        clearSelect(context) {
            context.commit('SET_INDEX', {
                yearsIndex: 0,
                monthIndex: 0
            })
        }
    },
    modules: {
        user: user,

    }
})
export default store